package com.jinlejia.erp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemScheduleLayoutBinding;
import com.squareup.picasso.Picasso;
import com.wang.avi.AVLoadingIndicatorView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;



/**
 * 日程安排（Adapter）
 */

public class VisitScheduleAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    private double Price;
    private onClickCallback callBack;
    private int type = 0;

    public VisitScheduleAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public VisitScheduleAdapter(ArrayList<Map<String, Object>> data, int type) {
        super(data);
        this.type = type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_schedule_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ItemScheduleLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);

        final Map<String, Object> map = mData.get(position);
        DecimalFormat df = new DecimalFormat("#.00");

        if (map.containsKey("imgUrl")) {
            Picasso.with(context).load(ApiConfig.MAGEHEADER_URL().concat(map.get("imgUrl").toString())).placeholder(R.mipmap.ic_no_img).into(binding.shootIcon);
        }
        if (map.containsKey("userImageUrl")) {
            Picasso.with(context).load(ApiConfig.MAGEHEADER_URL().concat(map.get("userImageUrl").toString())).placeholder(R.mipmap.ic_role_head).into(binding.hearIcon);
        }
        if (map.containsKey("price") && map.containsKey("type")) { //提交时间
            if (map.get("type").toString().equals("rent") || map.get("type").toString().equals("0")) {//租赁
                binding.money.setText(map.get("price").toString().concat("元"));
                if (map.containsKey("square") && map.containsKey("area") && map.containsKey("county")) {
                    binding.money.setText(map.get("price").toString().concat("元"));
                    binding.data.setText(map.get("county").toString().concat(" - " + map.get("area").toString()
                            .concat("丨" + map.get("square").toString().concat("㎡"))));
                }
            } else if (map.get("type").toString().equals("sale") || map.get("type").toString().equals("1")) {//买卖
                binding.money.setText(map.get("price").toString().concat("万元"));
                if (map.containsKey("square") && map.containsKey("area") && map.containsKey("county")) {
                    Price = Double.parseDouble(map.get("price").toString()) / Double.parseDouble(map.get("square").toString()) * 10000;
                    binding.data.setText(map.get("county").toString().concat(" - " + map.get("area").toString()
                            .concat("丨" + map.get("square").toString().concat("㎡").concat("丨" + df.format(Price) + "/㎡"))));
                }
            }
        }

        if (map.containsKey("estate") && map.containsKey("shsHouseType") && null!=map.get("shsHouseType")) //小区和厅室
            binding.address.setText(map.get("estate").toString().concat(" " + map.get("shsHouseType").toString()));

        if (type == 3) {  //预览
            if (map.containsKey("userImageUrl")) {
                binding.hearIcon.setVisibility(View.VISIBLE);
            } else {
                binding.hearIcon.setVisibility(View.GONE);
            }
        } else {
            binding.hearIcon.setVisibility(View.GONE);
        }
        binding.addIntroduceLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( null != callBack )
                callBack.onClickListener(position, mData);
            }
        });

        if (map.containsKey("recommendReason") && null!=map.get("recommendReason") && !TextUtils.isEmpty(map.get("recommendReason").toString())) {//推荐原因
            binding.explain.setText(map.get("recommendReason").toString());
        }else{
            binding.explain.setText("");
        }
        ArrayList<String> tags = new ArrayList<>();
        if (map.containsKey("tags") && null != map.get("tags") && !TextUtils.isEmpty(map.get("tags").toString()) ) {
            tags.addAll( Arrays.asList(map.get("tags").toString().split(";"))) ;
        }
        if (map.containsKey("houseType") && null != map.get("houseType") && !TextUtils.isEmpty(map.get("houseType").toString()))
            tags.addAll( Arrays.asList(map.get("houseType").toString().split(";"))) ;

        binding.tagView.setTags(tags);
//        if (tags.size() > 0) {
//            binding.introduceLayout.setVisibility(View.VISIBLE);
//            binding.tagView.setTags(tags);
//        } else {
//            binding.introduceLayout.setVisibility(View.GONE);
//        }

        binding.houseItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onHouseItemClickListener != null && map.containsKey("rscode"))
                    onHouseItemClickListener.OnHouseItemClick(map.get("rscode").toString());
            }
        });

        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }

    public interface onClickCallback {
        void onClickListener(int position, ArrayList<Map<String, Object>> data);
    }

    public void setCallBack(onClickCallback callBack) {
        this.callBack = callBack;
    }

    public interface OnHouseItemClickListener {
        void OnHouseItemClick(String code);
    }

    private OnHouseItemClickListener onHouseItemClickListener;

    public void setOnHouseItemClickListener(OnHouseItemClickListener listener) {
        this.onHouseItemClickListener = listener;
    }

}
